﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using System.Threading;
using System.Text;
using System.Linq;

using ESF.General;
using ESF.General.Entity;

#endregion


namespace ESF.Web
{
	public static class ListLoader
	{
		#region Seleccion
		
		public static void Mark (CheckBoxList pList, string pValue, bool pMark)
		{
			ListItem tmp;
			
			
			tmp = pList.Items.FindByValue (pValue);
			
			if (tmp != null)
			{
				tmp.Selected = pMark;
			}
		}
		
		
		public static void Enable (CheckBoxList pList, string pValue, bool pEnable)
		{
			ListItem tmp;
			
			
			tmp = pList.Items.FindByValue (pValue);
			
			if (tmp != null)
			{
				tmp.Enabled = pEnable;
			}
		}
		
		
		public static bool Select<L> (L pList, List<BasicReference> pItemsSelected) where L : ListControl
		{
			bool selection;
			
			
			if (DataHelper.IsEmpty (pItemsSelected)) { pList.SelectedIndex = -1; return false; }
		
			selection = false;
			
			foreach (ListItem item in pList.Items)
			{
				item.Selected = pItemsSelected.Exists (i => i.ID.ToString () == item.Value);
				
				if (item.Selected)
				{
					selection = true;
				}
			}
			
			return selection;
		}
		

		public static bool Select<L> (L pList, BasicReference pValue) where L : ListControl
		{
			if (pValue == null) { pList.SelectedIndex = -1; return false; }

			return Select (pList, pValue.ID);
		}

		
		public static void Select<L> (L pList, bool? pValue) where L : ListControl
		{
			if (pValue == null) 
			{
				if (!(pList is BulletedList))
				{
					pList.SelectedIndex = -1;
				}

				return;
			}
			
			Select<L> (pList, pValue.Value.ToString ());
		}
		
		
		public static bool Select<L> (L pList, int? pIDValue) where L : ListControl
		{
			if (pIDValue == null)
			{
				pList.SelectedIndex = -1; 
				return false;
			}
			
			return Select <L> (pList, pIDValue.Value);
		}
		

		public static bool Select<L> (L pList, int pIDValue) where L : ListControl
		{
			if (pIDValue == 0) { pList.SelectedIndex = -1; return false; }
			
			return Select<L> (pList, pIDValue.ToString());
		}
		
		
		public static bool Select<L> (L pList, string pValue) where L : ListControl
		{
			ListItem selected;


			pList.SelectedIndex = -1;

			if (string.IsNullOrEmpty (pValue)) { return false; }
			
			selected = pList.Items.FindByValue (pValue);
			
			if (selected != null)
			{
				if (!(pList is BulletedList))
				{
					pList.SelectedValue = pValue;
				}
				
				return true;
			}
			
			return false;
		}


		public static bool SelectByText<L> (L pList, string pText, bool pTryLIKE = false) where L : ListControl
		{
			ListItem selected;


			pList.SelectedIndex = -1;

			if (string.IsNullOrEmpty (pText)) { return false; }

			selected = pList.Items.FindByText (pText);
			
			if (selected != null)
			{
				if (!(pList is BulletedList))
				{
					pList.SelectedValue = selected.Value;
				}
				
				return true;
			}

			if (pTryLIKE)
			{
				pText = TextHelper.RemoveDiacritics (pText);

				foreach (ListItem item in pList.Items)
				{
					if (TextHelper.RemoveDiacritics (item.Text).IndexOf (pText, StringComparison.InvariantCultureIgnoreCase) != -1)
					{
						pList.SelectedValue = item.Value;

						return true;
					}
				}
			}
			
			return false;
		}


		public static void RemoveByText<L> (L pList, params string [] pValues) where L : ListControl
		{
			ListItem item;


			if (DataHelper.IsEmpty (pValues)) { return; }

			foreach (string val in pValues)
			{
				if (TextHelper.IsEmpty (val)) { continue; }

				item = pList.Items.FindByText(val.Trim ());

				if (item != null)
				{
					pList.Items.Remove (item);
				}
			}
		}
		
		
		public static void RemoveByValue<L> (L pList, params string [] pValues) where L : ListControl
		{
			ListItem item;


			if (DataHelper.IsEmpty (pValues)) { return; }

			foreach (string val in pValues)
			{
				item = pList.Items.FindByValue(val.Trim ());

				if (item != null)
				{
					pList.Items.Remove (item);
				}
			}
		}


		public static void RemoveByValueEnds<L> (L pList, string pValue) where L : ListControl
		{
			for (int i = 0; i < pList.Items.Count; i ++)
			{
				if (pList.Items [i].Value.EndsWith (pValue))
				{
					pList.Items.RemoveAt (i);
					
					i--;
				}
			}
		}


		public static void RemoveByValueStart<L>(L pList, string pValue) where L : ListControl
		{
			for (int i = 0; i < pList.Items.Count; i++)
			{
				if (pList.Items[i].Value.StartsWith(pValue))
				{
					pList.Items.RemoveAt(i);

					i--;
				}
			}
		}


		public static void IfOne<L> (L pList) where L : ListControl
		{
			if (pList.Items.Count > 2)
			{
				return;
			}

			RemoveSelectItem (pList);
		}
		
		
		public static void RemoveSelectItem<L> (L pList) where L : ListControl
		{
			RemoveByValue<L> (pList, string.Empty);
		}
		
		#endregion
		
		
		#region Limpieza
		
		public static void EnableIfHasItems<L> (L pList) where L : ListControl
		{
			pList.Enabled = (pList.Items.Count > 0);
		}
		
		
		public static void Clear<L> (L pList) where L : ListControl
		{
			pList.Items.Clear ();
			
			if (!(pList is BulletedList))
			{
			    pList.SelectedIndex = -1;
			}
			
			AddSelectItem<L> (pList);
		}
		
		
		public static void ClearDisable<L> (L pList) where L : ListControl
		{
			Clear <L> (pList);

			pList.Enabled = false;
		}
		
		#endregion
		
		
		#region Carga de Listados

		public static void LoadYESNOList<L>(L pList) where L : ListControl
		{
			LoadList (pList, TextHelper.YESNOList, "Code", "Name");
		}


		public static void RemoveIFNOThereIsValue<L> (L pList, params string [] pValues) where L : ListControl
		{
			int intI;

			bool blnEsta;


			if (DataHelper.IsEmpty (pValues)) { Clear (pList); return; }

			for (intI = 0; intI < pList.Items.Count; intI ++)
			{
				blnEsta = false;
			
				foreach (string value in pValues)
				{
					if (pList.Items [intI].Value == value)
					{
						blnEsta = true;
						break;
					}
				}
				
				if (!blnEsta) { pList.Items.RemoveAt (intI); }
			}
		}
		

		public static bool LoadListItems<L> (L pList, string pstrPairsKeyValue, char pSeparator) where L : ListControl
		{
			if (TextHelper.IsEmpty (pstrPairsKeyValue)) { return false; }

			return LoadListItems<L> (pList, pstrPairsKeyValue.Split (pSeparator));
		}

		
		public static bool LoadListItems<L> (L pList, params string [] pstrPairsKeyValue) where L : ListControl
		{
			int intI;


			Clear (pList);

			if (DataHelper.IsEmpty (pstrPairsKeyValue) || pstrPairsKeyValue.Length % 2 != 0)
			{
				return false;
			}


			for (intI = 0; intI < pstrPairsKeyValue.Length; intI += 2)
			{
				AddItem<L> (pList, pstrPairsKeyValue [intI], pstrPairsKeyValue [intI + 1]);
			}

			AddSelectItem (pList);
			
			return true;
		}
		
		
		private static void LoadListItems<L> (L pList, int pintStart, int pintEnd) where L : ListControl
		{
			Clear (pList);
			
			
			if (pintStart > pintEnd)
			{
				throw new GenericException (GenericException.CRI_INVALID_PARAMETER, string.Format ("El rango de numeros es invalido: {0} debe ser menor e igual que {1}", pintStart, pintEnd));
			}
		
			for (; pintStart <= pintEnd; pintStart ++)
			{
				AddItem<L> (pList, pintStart.ToString ());
			}
			
			AddSelectItem (pList);
		}
		
		
		public static void AddSelectItem<L> (L pList) where L : ListControl
		{
			AddSelectItem (pList, pList.Items.Count > 0 ? TextHelper.GeneralSelectItemText : "- No Hay -");
		}


		public static void AddAllItem<L> (L pList) where L : ListControl
		{
			AddSelectItem (pList, pList.Items.Count > 0 ? "- Todos -" : "- No Hay -");
		}
		
		
		public static void AddSelectItem<L> (L pList, string pstrSelectItemText) where L : ListControl
		{
			if (pList is DropDownList)
			{
				AddItem <L> (pList, pstrSelectItemText, TextHelper.SelectOneValue, 0);
			}
		}
		
		
		public static void AddSelectAllItem<L> (L pList) where L : ListControl
		{
			AddSelectAllItem <L> (pList, TextHelper.GeneralSelectAllItemText);
		}
		
		
		public static void AddSelectAllItem<L> (L pList, string pstrSelectAllItemText) where L : ListControl
		{
			StringBuilder stbMarkAllFunction;
		
		
			stbMarkAllFunction = new StringBuilder ();
		
			AddSelectItem <L> (pList, pstrSelectAllItemText);
		
			if (pList is CheckBoxList || pList is RadioButtonList)
			{	
				stbMarkAllFunction.AppendFormat ("var litAsterisk = document.getElementById ('{0}_0');", pList.ClientID);
				stbMarkAllFunction.AppendFormat ("var litAsteriskValue = litAsterisk.checked;");
				
				for (int i = 1; i < pList.Items.Count; i ++)
				{
					stbMarkAllFunction.AppendFormat ("litAsterisk = document.getElementById ('{0}_{1}');", pList.ClientID, i);
					stbMarkAllFunction.AppendFormat ("litAsterisk.checked = litAsteriskValue;");
				}
				
				pList.Items [0].Attributes.Add ("onclick", stbMarkAllFunction.ToString ());
			}
		}
		

		public static void AddItem<L> (L pList, List<string> pstrItemValues) where L : ListControl
		{
			if (DataHelper.IsEmpty(pstrItemValues))
			{
				return;
			}

			AddItem<L> (pList, pstrItemValues.ToArray ());
		}

		
		public static void AddItem<L> (L pList, params string [] pstrItemValues) where L : ListControl
		{
			if (DataHelper.IsEmpty (pstrItemValues))
			{
				return;
			}

			foreach (string itemValue in pstrItemValues)
			{
				AddItem<L> (pList, itemValue);
			}
		}


		public static void AddItem<L>(L pList, string pstrItemValue) where L : ListControl
		{
			AddItem<L>(pList, pstrItemValue, pstrItemValue, -1);
		}


		public static void AddItem<L> (L pList, string pstrItemValue, int pLocation) where L : ListControl
		{
			AddItem<L> (pList, pstrItemValue, pstrItemValue, pLocation);
		}


		public static void AddItem<L> (L pList, string pstrItemText, string pstrItemValue) where L : ListControl
		{
			AddItem<L> (pList, pstrItemText, pstrItemValue, -1);
		}


		public static void AddItem<L> (L pList, string pstrItemText, string pstrItemValue, int pLocation) where L : ListControl
		{
			ListItem selectItem;
			
		
			if (pList is ListBox && TextHelper.IsEmpty (pstrItemValue))
			{
				return;
			}

			selectItem = pList.Items.FindByValue (pstrItemValue);
			
			if (selectItem != null)
			{
				pList.Items.Remove (selectItem);
			}

			selectItem = new ListItem (pstrItemText.Trim (), pstrItemValue.Trim ());
			
			if (pLocation < 0)
			{
				pList.Items.Add (selectItem);
			}
			else
			{
				pList.Items.Insert (0, selectItem);
			}
		}


        public static void ChangeItem<L>(L pList, string pstrItemText, string pstrItemValue) where L : ListControl
        {
            ListItem selectItem;


            selectItem = pList.Items.FindByValue(pstrItemValue);

            if (selectItem != null)
            {
                selectItem.Text = pstrItemText;
                selectItem.Value = pstrItemValue;
            }
        }


		public static void SelectAll<L> (L pList) where L : ListControl
		{
			for (int i = 0; i < pList.Items.Count; i++)
			{
				pList.Items [i].Selected = true;
			}
		}


        public static void ChangeSelectItemText<L>(L pList, string pstrItemText) where L : ListControl
        {
            ChangeItem<L>(pList, pstrItemText, string.Empty);
        }		


		public static bool LoadList<L> (L pList, DataSet pSet) where L : ListControl
		{
			return LoadList (pList, DataHelper.GetFirstDataTable (pSet));
		}

		
		public static bool LoadList<L> (L pList, DataTable pTable) where L : ListControl
		{
			Clear (pList);
		
			if (DataHelper.IsEmpty (pTable)) { return false; }

			if (pTable.Columns.Count == 1)
			{
				return LoadList<L> (pList, pTable, pTable.Columns [0].ColumnName, pTable.Columns [0].ColumnName);
			}
			
			return LoadList<L> (pList, pTable, pTable.Columns [0].ColumnName, pTable.Columns [1].ColumnName);
		}


		public static bool LoadList<L> (L pList, DataTable pTable, string pstrValueFieldName, string pTextFieldName) where L : ListControl
		{
			return LoadList<L> (pList, pTable, new string [] { pstrValueFieldName }, pTextFieldName, null);
		}


		public static bool LoadList<L> (L pList, DataTable pTable, string [] pValueFieldNames, string pTextFieldName) where L : ListControl
		{
			return LoadList<L> (pList, pTable, pValueFieldNames, pTextFieldName, null);
		}


		public static bool LoadList<L>
		(
			L pList, DataTable pTable, string [] pValueFieldNames, string pTextFieldName, string pSelectedValue
		) where L : ListControl
		{
			ListItem objItem;
			
			int intI;
		
		
			Clear (pList);

			if (DataHelper.IsEmpty (pTable)) { return false; }

			foreach (DataRow row in pTable.Rows)
			{
				objItem = new ListItem (row [pTextFieldName].ToString ().Trim ());
				
				objItem.Value = row [pValueFieldNames [0]].ToString ();
				
				for (intI = 1; intI < pValueFieldNames.Length; intI ++)
				{
					objItem.Value += '|';
					
					if (!row.IsNull (pValueFieldNames [intI]))
					{
						objItem.Value += row [pValueFieldNames [intI]].ToString ();
					}
				}
				
				AddItem (pList, objItem.Text, objItem.Value);
			}
			
			AddSelectItem<L> (pList);
		
			if (!(pList is BulletedList))
			{
				if (string.IsNullOrEmpty (pSelectedValue)) { pList.SelectedIndex = -1; }
				else { pList.SelectedValue = pSelectedValue; }
			}
			
			return true;
		}


		public static bool LoadListTexts<L> (L pList, string pItemSeparatedList, char pSeparator) where L : ListControl
		{
			Clear (pList);

			if (TextHelper.IsEmpty (pItemSeparatedList)) { return false; }

			return LoadListTexts (pList, pItemSeparatedList.Split (pSeparator));
		}


		public static bool LoadListTexts<L> (L pList, List<string> pTexts) where L : ListControl
		{
			Clear(pList);

			if (DataHelper.IsEmpty(pTexts)) { return false; }


			foreach (string text in pTexts)
			{
				AddItem<L>(pList, text);
			}

			AddSelectItem<L>(pList);

			return true;
		}


		public static bool LoadListTexts<L> (L pList, params string [] pTexts) where L : ListControl
		{
			Clear (pList);

			if (DataHelper.IsEmpty (pTexts)) { return false; }

			
			foreach (string text in pTexts)
			{
				AddItem<L> (pList, text);
			}

			AddSelectItem<L> (pList);
			
			return true;
		}


		public static bool LoadList<L>(L pList, List<BasicReferenceType> pItems) where L : ListControl
		{
			Clear(pList);

			if (DataHelper.IsEmpty(pItems)) { return false; }

			foreach (BasicReferenceType item in pItems)
			{
				AddItem(pList, item.Name, item.ID.ToString());
			}

			AddSelectItem<L>(pList);

			return true;
		}
		
		
		public static bool LoadList<L> (L pList, string pstrDataSetXMLFirstTable, string pstrValueFieldName, string pTextFieldName) where L : ListControl
		{
			return LoadList<L> (pList, DataHelper.ToDataSet (pstrDataSetXMLFirstTable), pstrValueFieldName, pTextFieldName);
		}
		
		
		public static bool LoadList<L> (L pList, DataSet pdtsSetFirstTable, string pstrValueFieldName, string pTextFieldName) where L : ListControl
		{
			return LoadList<L> (pList, DataHelper.GetFirstDataTable (pdtsSetFirstTable), pstrValueFieldName, pTextFieldName);
		}
		

		public static bool LoadList<L> (L pList, params string [] pstrPairsKeyValue) where L : ListControl
		{
			Clear (pList);

			return LoadListItems (pList, pstrPairsKeyValue);
		}
				
		
		public static bool LoadList<L> (L pList, List<BasicReference> pobjObjectList) where L : ListControl
		{
			return LoadList<L, BasicReference> (pList, pobjObjectList, "ID", "Name");
		}
		
		
		public static bool LoadList<L,OL> (L pList, List<OL> pobjObjectList, string pstrValueFieldName, string pTextFieldName) where L : ListControl
		{
			return LoadList<L,OL> (pList, pobjObjectList, pstrValueFieldName, pTextFieldName, null);
		}
		
		
		public static bool LoadList<L,OL> (L pList, List<OL> pobjObjectList, string pstrValueFieldName, string pTextFieldName, string pstrSelectedValue) where L : ListControl
		{
			Clear (pList);
			
			if (DataHelper.IsEmpty (pobjObjectList))
			{
				return false;
			}
			
			pList.DataTextField = pTextFieldName;
			pList.DataValueField = pstrValueFieldName;
			pList.DataSource = pobjObjectList;
			
			pList.DataBind ();
			
			if (!(pList is BulletedList))
			{
				if (string.IsNullOrEmpty (pstrSelectedValue)) { pList.SelectedIndex = -1; }
				else { pList.SelectedValue = pstrSelectedValue; }
			}
		
			AddSelectItem (pList);
		
		    return true;
		}


		public static bool LoadListTextToString<L,OL> (L pList, List<OL> pobjObjectList, string pstrValueFieldName) where L : ListControl
		{
			return LoadListTextToString (pList, pobjObjectList, pstrValueFieldName, string.Empty);
		}


		public static bool LoadListTextToString<L,OL> (L pList, List<OL> pobjObjectList, string pstrValueFieldName, string pTextFieldName) where L : ListControl
		{
			return LoadListTextToString<L, OL>(pList, pobjObjectList, new string[] { pstrValueFieldName }, pTextFieldName);
		}


		public static bool LoadListTextToString<L, OL>(L pList, List<OL> pobjObjectList, string[] pstrValueFieldNames, string pTextFieldName) where L : ListControl
		{
			Type type;

			StringBuilder tempValue = new StringBuilder ();


			Clear(pList);
			
			if (DataHelper.IsEmpty (pobjObjectList))
			{
				return false;
			}

			type = typeof(OL);

			foreach (OL item in pobjObjectList)
			{
				tempValue.Length = 0;

				foreach (string valueFieldName in pstrValueFieldNames)
				{
					tempValue.AppendFormat("{0}{1}", tempValue.Length > 0 ? "|" : string.Empty, type.GetProperty(valueFieldName).GetValue(item, null));
				}

				if (string.IsNullOrEmpty (pTextFieldName))
				{
					AddItem (pList, item.ToString(), tempValue.ToString ());
				}
				else
				{
					AddItem(pList, type.GetProperty(pTextFieldName).GetValue(item, null).ToString(), tempValue.ToString());
				}
			}

			AddSelectItem(pList);

			return true;
		}
		
		#endregion

		
		#region Movimiento de Elementos
		
		public static void OrderElementsByTexts<L> (L pList) where L : ListControl
		{
			List<ListItem> items;

			ListItem current;


			items = new List<ListItem> (pList.Items.Count);

			foreach (ListItem oi in pList.Items)
			{
				items.Add (oi);
			}

			pList.Items.Clear ();

			current = items.Find (i => i.Value.Length == 0);

			if (current != null)
			{
				pList.Items.Add (current);

				items.Remove (current);
			}

			pList.Items.AddRange (items.OrderBy (i => i.Text).ToArray ());
		}


		public static void RemoveEqualElements<L,OL> (L pListRemove, OL pListCompare) 
			where L : ListControl
			where OL : ListControl
		{
			int intI;


			for (intI = 0; intI < pListCompare.Items.Count; intI++)
			{
				if (pListRemove.Items.FindByValue (pListCompare.Items [intI].Value) != null)
				{
					pListRemove.Items.Remove (pListCompare.Items [intI]);
				}
			}
		}


		public static void CopySelected(ListBox pSource, ListBox pDestination, bool pblnFilterUniques = true)
		{
			CopySelected (pSource.GetSelectedIndices (), pSource.Items, pDestination.Items, pblnFilterUniques);
		}

				
		public static void CopySelected (int [] pintSourceSelectedIndexes, ListItemCollection pobjSourceListItems, ListItemCollection pobjDestinationListItems, bool pblnFilterUniques = true)
		{
			foreach (int intSelectedIndex in pintSourceSelectedIndexes)
			{
				if (pblnFilterUniques)
				{
					if (pobjDestinationListItems.FindByValue (pobjSourceListItems [intSelectedIndex].Value) == null)
					{
						pobjDestinationListItems.Add (pobjSourceListItems [intSelectedIndex]);
					}
				}
				else
				{
					pobjDestinationListItems.Add (pobjSourceListItems [intSelectedIndex]);
				}
			}
		}


		public static void MoveSelected (ListBox pSource, ListBox pDestination, bool pFilterUniques = true)
		{
			MoveSelected (pSource, pDestination, pSource.GetSelectedIndices (), pFilterUniques);
		}
		

		public static void MoveSelected<L> (L pobjSourceList, L pobjDestinationList, int [] pintSelectedIndexes = null, bool pblnFilterUniques = true, string pstrAditionalValueSeparator = "", string pstrAditionalValue = "", bool pblnAddAditionalValueSeparator = false) where L : ListControl
		{
			int intAdjust;
			int intAdjustIndex;
			
			string pstrTextValueForAdd;
			
			ListItem objItem;


			if (DataHelper.IsEmpty(pintSelectedIndexes))
			{
				return;
			}

			pobjDestinationList.Enabled = true;

			intAdjust = 0;
			
			pstrTextValueForAdd = " " + pstrAditionalValueSeparator + " " + pstrAditionalValue;
					
			
			foreach (int intSelectedIndex in pintSelectedIndexes)
			{
				intAdjustIndex = intSelectedIndex - intAdjust;

				objItem = pobjSourceList.Items [intAdjustIndex];

				if (pblnAddAditionalValueSeparator && !objItem.Text.EndsWith (pstrTextValueForAdd)) { objItem.Text += pstrTextValueForAdd; }

				// Hay que remover el Value adicional
				if (string.IsNullOrEmpty (pstrAditionalValue)) 
				{
					objItem.Text = objItem.Text.Split (pstrAditionalValueSeparator [0]) [0]; 
					objItem.Value = objItem.Value.Split (pstrAditionalValueSeparator [0]) [0]; 
				}
				else if (!objItem.Value.EndsWith (pstrAditionalValueSeparator + pstrAditionalValue)) 
				{ 
					objItem.Value += pstrAditionalValueSeparator + pstrAditionalValue; 
				}

				if (pblnFilterUniques)
				{
					if (pobjDestinationList.Items.FindByValue (objItem.Value + pstrAditionalValueSeparator + pstrAditionalValue) == null)
					{
						pobjDestinationList.Items.Add (pobjSourceList.Items [intAdjustIndex]);
						pobjSourceList.Items.RemoveAt (intAdjustIndex);

						intAdjust++;
					}
				}
				else
				{
					pobjDestinationList.Items.Add (pobjSourceList.Items [intAdjustIndex]);
					pobjSourceList.Items.RemoveAt (intAdjustIndex);

					intAdjust++;
				}
			}
			
			pobjSourceList.SelectedIndex = -1;
			pobjDestinationList.SelectedIndex = -1;
		}


		public static void MoveAll<L> (L pobjSourceList, L pobjDestinationList, bool pblnFilterUniques) where L : ListControl
		{
			int intI;


			pobjDestinationList.Enabled = true;


			for (intI = 0; intI < pobjSourceList.Items.Count && pobjSourceList.Items.Count > 0; intI++)
			{
				if (pblnFilterUniques)
				{
					if (pobjDestinationList.Items.FindByValue (pobjSourceList.Items [intI].Value) == null)
					{
						pobjDestinationList.Items.Add (pobjSourceList.Items [intI]);
						pobjSourceList.Items.Remove (pobjSourceList.Items [intI--]);
					}
				}
				else
				{
					pobjDestinationList.Items.Add (pobjSourceList.Items [intI]);
					pobjSourceList.Items.Remove (pobjSourceList.Items [intI--]);
				}
			}
		}


		


		public static void RemoveSelected<L> (L pList) where L : ListControl
		{
			for (int i = 0; i < pList.Items.Count; i++)
			{
				if (pList.Items [i].Selected)
				{
					pList.Items.RemoveAt (i);

					i--;
				}
			}
		}


		public static void RemoveSelected<L> (L pList, int [] pintSelectedIndexes) where L : ListControl
		{
			int intAdjust;


			intAdjust = 0;

			foreach (int intSelectedIndex in pintSelectedIndexes)
			{
				if (pList.Items [intSelectedIndex - intAdjust].Selected)
				{
					pList.Items.RemoveAt (intSelectedIndex - intAdjust);

					intAdjust++;
				}
			}
		}


		public static void MoveSelectedUp<L> (L pList) where L : ListControl
		{
			int intI;
			
			ListItem objItem;
		
		
			for (intI = 1; intI < pList.Items.Count; intI ++)
			{
				if (pList.Items [intI].Selected)
				{
					objItem = pList.Items [intI];
					
					pList.Items.RemoveAt (intI);
					pList.Items.Insert (intI - 1, objItem);
				}
			}
		}


		public static void MoveSelectedDown<L> (L pList) where L : ListControl
		{
			int intI;

			ListItem objItem;


			for (intI = 0; intI < pList.Items.Count - 1; intI++)
			{
				if (pList.Items [intI].Selected)
				{
					objItem = pList.Items [intI];

					pList.Items.RemoveAt (intI);
					pList.Items.Insert (intI + 1, objItem);
				}
			}
		}

		#endregion


		#region Extracion de Informacion

		public static List<string> GetAllValues<L> (L pList) where L : ListControl
		{
			List<string> result = new List<string> ();

			
			foreach (ListItem value in pList.Items)
			{
				result.Add (value.Value);
			}

			return result;
		}


		public static List<string> GetAllTexts<L> (L pList) where L : ListControl
		{
			List<string> result = new List<string>();

			
			foreach (ListItem value in pList.Items)
			{
				result.Add(value.Text);
			}

			return result;
		}


		public static string GetSelectedText<L> (L pList) where L : ListControl
		{
			List<string> result = new List<string>();


			if (pList.SelectedValue.Length == 0)
			{
				return string.Empty;
			}
			
			return pList.SelectedItem.Text;
		}


		public static List<BasicReference> GetSelectedItemsBasRef<L> (L pList) where L : ListControl
		{
			List<BasicReference> result = new List<BasicReference>();


			foreach (ListItem item in GetSelectedItems<L>(pList))
			{
				result.Add((new BasicReference() { ID = int.Parse(item.Value) }));
			}

			return result;
		}


		public static List<ListItem> GetSelectedItems<L> (L pList) where L : ListControl
		{
			List<ListItem> result = new List<ListItem>();


			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					result.Add(item);
				}
			}

			return result;
		}


		public static List<string> GetSelectedTexts<L> (L pList) where L : ListControl
		{
			List<string> result = new List<string>();


			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					result.Add (item.Text);
				}
			}

			return result;
		}

		
		public static string GetSelectedValuesCVS<L> (L pList) where L : ListControl
		{
			return GetSelectedValuesXVS<L> (pList, ',');
		}
		
		
		public static string GetSelectedValuesXVS<L> (L pList, char pSeparatorChar) where L : ListControl
		{
			StringBuilder stbResult;


			stbResult = new StringBuilder ();

			
			if (pList.SelectedIndex == -1)
			{
				return string.Empty;
			}

			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					stbResult.AppendFormat ("{0}{1}", item.Value, pSeparatorChar);
				}
			}

			if (stbResult.Length > 0)
			{
				stbResult.Length --;
			}

			return stbResult.ToString ();
		}


		public static BasicReferenceType GetSelectedBasicReferenceType<L> (L pList) where L : ListControl
		{
			if (TextHelper.IsEmpty (pList.SelectedValue))
			{
				return null;
			}

			return new BasicReferenceType () { ID = TextHelper.GetInt (pList.SelectedValue), Name = pList.SelectedItem.Text };
		}


		public static BasicReference GetSelectedBasicReference<L> (L pList) where L : ListControl
		{
			string selectedValue;

			int indexSeparator;


			if (TextHelper.IsEmpty (pList.SelectedValue))
			{
				return null;
			}


			indexSeparator = pList.SelectedValue.IndexOf ('|');

			if (indexSeparator != -1)
			{
				selectedValue = pList.SelectedValue.Substring (0, indexSeparator);
			}
			else
			{
				selectedValue = pList.SelectedValue;
			}

			return new BasicReference () { ID = TextHelper.GetNullableInt (selectedValue), Name = pList.SelectedItem.Text };
		}


		public static BasicReference GetSelectedBasicReferenceWithCode<L> (L pList) where L : ListControl
		{
			if (TextHelper.IsEmpty (pList.SelectedValue))
			{
				return null;
			}

			return new BasicReference () { Code = pList.SelectedValue, Name = pList.SelectedItem.Text };
		}
		
		
		public static List<string> GetSelectedValues<L> (L pList) where L : ListControl
		{
			List<string> result = new List<string>();


			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					result.Add (item.Value);
				}
			}

			return result;
		}


		public static List<int> GetSelectedValuesInt32<L> (L pList) where L : ListControl
		{
			List<int> result = new List<int>();


			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					result.Add(int.Parse(item.Value));
				}
			}

			return result;
		}
		
		
		public static List<uint> GetSelectedValuesUInt32<L> (L pList) where L : ListControl
		{
			List<uint> result = new List<uint>();


			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					result.Add(uint.Parse(item.Value));
				}
			}

			return result;
		}
		
		
		public static List<byte> GetSelectedValuesByte<L> (L pList) where L : ListControl
		{
			List<byte> result = new List<byte>(); 
			
			
			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					result.Add(byte.Parse(item.Value));
				}
			}

			return result;
		}
		
		
		public static List<short> GetSelectedValuesShort<L> (L pList) where L : ListControl
		{
			List<short> result = new List<short>();


			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					result.Add (short.Parse (item.Value));
				}
			}

			return result;
		}
		
		
		public static List<ushort> GetSelectedValuesUShort<L> (L pList) where L : ListControl
		{
			List<ushort> result = new List<ushort> ();			


			foreach (ListItem item in pList.Items)
			{
				if (item.Selected)
				{
					result.Add (ushort.Parse (item.Value));
				}
			}

			return result;
		}
		
		#endregion
		

		#region CheckBoxList

		public static void CheckItems (CheckBoxList pchkList, bool pblnCheck)
		{
			foreach (ListItem chkItem in pchkList.Items)
			{
				chkItem.Selected = pblnCheck;
			}
		}

		#endregion


		#region Enumeracion

		public static void LoadList<L> (L pList, Type ptypTipoEnumeracion) where L : ListControl
		{
			LoadList (pList, ptypTipoEnumeracion, -1);
		}
		
		
		public static void LoadList<L> (L pList, Type ptypTipoEnumeracion, int pintSelectedValue) where L : ListControl
		{
			string [] strNombresEnum;

			Array arrValueesEnum;

			int intI;

			
			Clear (pList);
			
			strNombresEnum = Enum.GetNames (ptypTipoEnumeracion);

			arrValueesEnum = Enum.GetValues (ptypTipoEnumeracion);

			for (intI = 0; intI < strNombresEnum.Length; intI++)
			{
				AddItem <L> (pList, strNombresEnum [intI], Convert.ToInt32 (arrValueesEnum.GetValue (intI)));
			}

			AddSelectItem <L> (pList);
		}

		#endregion


		#region Meses, Años

		public static void LoadListAlphabetWithEnie <L> (L pList) where L : ListControl
		{
			Clear (pList);
		
			LoadListItems (pList, TextHelper.ALPHABETWITHENIE);		
			AddSelectItem (pList, "-");
		}


		public static void LoadListAlphabet <L> (L pList) where L : ListControl
		{
			Clear (pList);
		
			LoadListItems (pList, TextHelper.ALPHABET);		
			AddSelectItem (pList, "-");
		}


		public static void LoadListMonths<L> (L pList) where L : ListControl
		{
			Clear (pList);
		
			LoadListItems (pList, TextHelper.GeneralMonthListText);		
			AddSelectItem (pList, TextHelper.GeneralSelectItemMonthText);
		}
		
		
		public static void LoadListTrimesterMonths<L> (L pList) where L : ListControl
		{
			Clear (pList);
		
			LoadListItems (pList, TextHelper.GeneralTrimesterMonthListText);		
			AddSelectItem (pList, TextHelper.GeneralSelectItemMonthText);
		}


		private static List<BasicReference> GenerateListRange(int pStart, int pEnd, int pRangeLength = 1)
		{
			string temp;

			List<BasicReference> result = new List<BasicReference>();


			for (; pStart <= pEnd; pStart += pRangeLength)
			{
				temp = string.Format("{0} - {1}", pStart, pStart + pRangeLength);

				result.Add (new BasicReference() { Code = temp, Name = temp });
			}

			if (pStart < pEnd + pRangeLength)
			{
				temp = string.Format("{0} - {1}", pStart, pStart + pRangeLength);

				result.Add (new BasicReference() { Code = temp, Name = temp });
			}

			return result;
		}


		public static void LoadGeneratedRange<L> (L pList, int pStart, int pEnd, int pRangeLength = 1) where L : ListControl
		{
			LoadList(pList, GenerateListRange (pStart, pEnd, pRangeLength), "Code", "Name");
		}


		public static void LoadGeneratedRangeDesc<L> (L pList, int pStart, int pEnd, int pRangeLength = 1) where L : ListControl
		{
			List<BasicReference> items;


			items = GenerateListRange (pStart, pEnd, pRangeLength);
			
			items.Reverse ();
			
			LoadList(pList, items, "Code", "Name");
		}
		
		
		public static void LoadListRange<L> (L pList, int pStart, int pEnd) where L : ListControl
		{
			Clear<L>(pList);
			
			LoadList <L>(pList, TextHelper.GetRangeList (pStart, pEnd));
		}


		public static void LoadListYears<L> (L pList) where L : ListControl
		{
			LoadListYears (pList, null);
		}


		public static void LoadListYears<L> (L pList, int? pintYearSelected) where L : ListControl
		{
			if (pintYearSelected != null)
			{		
				if (pintYearSelected.Value >= DateTime.Now.Year)
				{
					LoadListYears (pList, ReadConfig.ListYearsStart, pintYearSelected.Value, pintYearSelected);
				}
				else
				{
					LoadListYears (pList, ReadConfig.ListYearsStart, DateTime.Now.Year, pintYearSelected);
				}
			}
			else
			{
				LoadListYears (pList, DateTime.Now.Year);
			}
		}
		
		
		public static void LoadListYears<L> (L pList, int pEndYear) where L : ListControl
		{
			LoadListYears (pList, ReadConfig.ListYearsStart, pEndYear, null);
		}
		
		
		public static void LoadListYears<L> (L pList, int pStartYear, int pEndYear) where L : ListControl
		{
			LoadListYears (pList, pStartYear, pEndYear, null);
		}


		public static void LoadListYears<L> (L pList, int pStartYear, int pEndYear, int? pintYearSelected) where L : ListControl
		{
			Clear (pList);
		
			LoadListItems (pList, pStartYear, pEndYear);
			AddSelectItem (pList, TextHelper.GeneralSelectItemYearText);

			if (pintYearSelected != null)
			{
				pList.SelectedValue = pintYearSelected.ToString ();
			}
		}
		
		
		public static void LoadListPageSize<L> (L pList) where L : ListControl
		{
			Clear<L> (pList);
			
			RemoveSelectItem<L> (pList);

			AddItem(pList, "500", "500");
			AddItem(pList, "300", "300");
			AddItem(pList, "100", "100");
			AddItem (pList, "50", "50");
			AddItem (pList, "30", "30");
			AddItem (pList, "20", "20");
			AddItem (pList, "10", "10");
			AddItem (pList, "5", "5");
			AddItem (pList, "1", "1");
			
			Select (pList, "10");
		}
		
		
		public static void LoadListDays<L> (L pList) where L : ListControl
		{
			LoadListDays (pList, null);
		}
		
		
		public static void LoadListDays<L> (L pList, int? pintDaySelected) where L : ListControl
		{
			Clear (pList);
		
			LoadListItems (pList, 1, 31);
			AddSelectItem (pList, TextHelper.GeneralSelectItemDayText);
			
			if (pintDaySelected != null)
			{
				pList.SelectedValue = pintDaySelected.ToString ();
			}
		}

		#endregion
		
		
		#region Adicion de Funcion de JS
		
		public static void AddJSCallToItemsWithValue<L> (L pList, string pJSEvent, string pJSFunctionName) where L : ListControl
		{
			foreach (ListItem li in pList.Items)
			{
				li.Attributes.Add (pJSEvent, pJSFunctionName + " (value)");
			}
		}
		
		#endregion
	}
}